﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class DispatchRuleDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbIsActive;
        protected DropDownList ddlTaskPickCode;
        protected DropDownList ddlTaskType;
        protected HtmlForm F2213;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblDesc;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblTaskPickCode;
        protected Label lblTaskRuleDetail;
        protected Label lblTaskRuleID;
        protected Label lblTaskType;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDes;
        protected TextBox txtLineID;
        protected TextBox txtTaskRuleID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("DispatchRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtTaskRuleID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                TaskRuleDetailInfo taskRuleDetail = new TaskRuleDetailInfo {
                    TaskRuleID = this.txtTaskRuleID.Text.Trim(),
                    TaskStrategy = "",
                    TaskType = this.ddlTaskType.SelectedValue,
                    Descr = this.txtDes.Text.Trim(),
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    TaskPickCode = "",
                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                if (this.hdnType.Value == "edit")
                {
                    taskRuleDetail.UpdatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(taskRuleDetail, "TASK_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(taskRuleDetail, "TASK_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_005") + ex.Message;
                return;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("DispatchRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtTaskRuleID.Text.Trim())));
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                TaskRuleDetailInfo taskRuleDetail = new TaskRuleDetailInfo {
                    TaskRuleID = this.txtTaskRuleID.Text.Trim(),
                    TaskStrategy = "",
                    TaskType = this.ddlTaskType.SelectedValue,
                    Descr = this.txtDes.Text.Trim(),
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    TaskPickCode = "",
                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(taskRuleDetail, "TASK_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2450_005") + ex.Message;
                return;
            }
            if (success)
            {
                this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                if (this.ddlTaskType.Items.Count > 0)
                {
                    this.ddlTaskType.SelectedValue = this.ddlTaskType.Items[0].Value;
                }
                this.txtDes.Text = "";
                this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                this.chbIsActive.Checked = true;
            }
        }

        private void InitTaskRuleDetailDropDownLit()
        {
            this.InitTaskType(this.ddlTaskType);
        }

        private void InitTaskType(DropDownList ddl)
        {
            List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> taskType = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("TaskType");
            ddl.DataSource = taskType;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void LoadData()
        {
            this.InitTaskRuleDetailDropDownLit();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string taskRuleID = base.Request.QueryString["ID"].ToString();
                this.txtTaskRuleID.Text = taskRuleID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string LineID = base.Request.QueryString["SeqNO"].ToString();
                this.txtLineID.Text = LineID;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.btnSaveContinue.Enabled = false;
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetDispatchRuleDetail2From();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDispatchRuleDetail2From()
        {
            if ((this.txtTaskRuleID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                TaskRuleDetailInfo taskRuleDetail = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtTaskRuleID.Text.Trim(), this.txtLineID.Text.Trim(), "", "TASK_RULE_DETAIL") as TaskRuleDetailInfo;
                if (taskRuleDetail != null)
                {
                    this.ddlTaskType.SelectedValue = taskRuleDetail.TaskType;
                    this.txtDes.Text = taskRuleDetail.Descr;
                    this.chbIsActive.Checked = taskRuleDetail.IsActive == "Y";
                    this.txtTaskRuleID.Enabled = false;
                    this.txtLineID.Enabled = false;
                }
            }
        }
    }
}

